﻿using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;

namespace YmtAuth.Domain.EventHistory
{
    /// <summary>
    /// 用户登录历史
    /// </summary>
    public class YmtUserLoginEvent : Entity<string>
    {
        public string sLoginId { get; private set; }
        public int iAction { get; private set; }
        [BsonIgnoreIfNull]
        public string sLoginIp { get; private set; }
        [BsonIgnoreIfNull]
        public string sSummary { get; private set; }
        public DateTime dAddTime { get; private set; }
        /// <summary>
        /// 登录客户端类型：www.ymatou.com ，app，dms，xxx.admin,ali,weibo
        /// </summary>
        [BsonIgnoreIfNull]
        public string sClientType { get; private set; }

        [BsonIgnoreIfDefault]
        public double RunTime { get; private set; }

        [BsonIgnoreIfNull]
        public string CSource { get; private set; }

        [BsonIgnoreIfNull]
        public string TSource { get; private set; }

        [BsonIgnoreIfNull]
        public string Desc { get; private set; }

        [BsonIgnoreIfNull]
        public string DeviceId { get; private set; }

        [BsonIgnoreIfNull]
        public string ClientId { get; private set; }

        public int ResponseCode { get; private set; }

        public YmtUserLoginEvent(string loginId, int action
            , string loginIp, string summary, string clientType, double runTime, string cSource = null,
            string tSource = null, string description = null, string deviceid = null, string clientId = null
            , int responseCode = 0)
        {
            this.Id = Guid.NewGuid().ToString("N");
            this.sLoginId = loginId.ToLower();
            this.iAction = action;
            this.sLoginIp = loginIp;
            this.sSummary = summary;
            this.sClientType = clientType;
            this.dAddTime = DateTime.Now;
            this.RunTime = runTime;
            this.Desc = description;
            this.CSource = string.IsNullOrEmpty(cSource) ? null : cSource.ToLower();
            this.TSource = string.IsNullOrEmpty(tSource) ? null : tSource.ToLower();
            this.DeviceId = deviceid;
            this.ClientId = clientId;
            this.ResponseCode = responseCode;
        }

        protected override IEnumerable<object> CompareValues()
        {
            yield return this.Id;
        }

        protected YmtUserLoginEvent()
        {
        }
    }
}
